package com.paireach.szwolaiyun.newFragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.WindowManager;

import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;
import com.paireach.szwolaiyun.util.BarCodeUtil;

import java.lang.ref.WeakReference;

/**
 * 生成一维码的父fragment
 */
public class BarCoderFragment extends BaseFragment {

    private DrawBarCode runnable;
    private BarCodeUtil util;
    private Bitmap bitmap;
    private MyHandler handler;
    private String numberStr;

    private OnBarCodeGenerateListener l;

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            bitmap.recycle();
        } catch (Exception e) {
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new MyHandler(this);
        runnable = new DrawBarCode(this);
        WindowManager wm = getActivity().getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        util = new BarCodeUtil(width * 2 / 3, height / 6);
    }

    public void startGenerateBarCode(String number) {
        this.numberStr = number;
        Thread drawThread = new Thread(runnable);
        drawThread.start();
    }

    private static class DrawBarCode implements Runnable {

        WeakReference<BarCoderFragment> mm;

        DrawBarCode(BarCoderFragment fragment) {
            mm = new WeakReference<>(fragment);
        }

        @Override
        public void run() {
            BarCoderFragment fragment = mm.get();
            if (fragment != null) {
                try {
                    fragment.bitmap = fragment.util.bitmap1(fragment.numberStr);
                    Message message = fragment.handler.obtainMessage();
                    message.obj = fragment.bitmap;
                    fragment.handler.sendMessage(message);
                } catch (Exception e) {
                    Log.e("barCoder", "draw bar code failed." + e.toString());
                }
            }
        }
    }

    public void setOnBarCodeGeneratedListener(OnBarCodeGenerateListener l) {
        this.l = l;
    }

    private static final class MyHandler extends Handler {

        private WeakReference<BarCoderFragment> mm;

        public MyHandler(BarCoderFragment fragment) {
            mm = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            BarCoderFragment fragment = mm.get();
            if (fragment != null) {
                if (fragment.l != null) {
                    fragment.l.onBarCodeGenerated((Bitmap) msg.obj);
                }
            }
        }
    }

    public interface OnBarCodeGenerateListener {
        void onBarCodeGenerated(Bitmap bitmap);
    }
}
